/*******************************************************************************
 * Copyright (c) 2014 Francesco Gabbrielli.
 * 
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Public License v2.0
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 * 
 * Contributors:
 *     Francesco Gabbrielli - initial API and implementation
 ******************************************************************************/
package it.francescogabbrielli.android.solidalapp;

import it.francescogabbrielli.android.solidalapp.file.RequestFileActivity;
import it.francescogabbrielli.android.solidalapp.file.ResponseFileActivity;
import it.francescogabbrielli.android.solidalapp.home.HomeActivity;
import it.francescogabbrielli.android.solidalapp.response.ResponseActivity;
import android.app.Activity;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

public enum IntentAction {
	
	NULL_ACTION					(null),
	
	/** Register device to GCM */
	GCM_REGISTER_DEVICE			(GCMService.class),
	/** Chat message from request side (needs to use endpoints) */
	GCM_SEND_REQUEST_CHAT		(EndpointsService.class),
	/** Chat message from response side (needs to use endpoints) */
	GCM_SEND_RESPONSE_CHAT		(EndpointsService.class),
	
	/** Message received from GCM */
	GCM_REQUEST_RECEIVED		(ResponseActivity.class),
	/** Response received from GCM */
	GCM_RESPONSE_RECEIVED		(HomeActivity.class),
	/** Feedback received from GCM */
	GCM_FEEDBACK_RECEIVED		(null),

	/** Chat message received by request side */
	GCM_REQUEST_CHAT_RECEIVED	(RequestFileActivity.class),
	/** Chat message received by response side */
	GCM_RESPONSE_CHAT_RECEIVED	(ResponseFileActivity.class),
	/** Chat message sent by request side */
	GCM_REQUEST_CHAT_SENT		(RequestFileActivity.class),
	/** Chat message sent by response side */
	GCM_RESPONSE_CHAT_SENT		(ResponseFileActivity.class),
	
	/** Get profile data */
	ENDPOINTS_GET_PROFILE		(EndpointsService.class),
	/** Insert new profile data */
	ENDPOINTS_INSERT_PROFILE	(EndpointsService.class),
	/** Update profile data */
	ENDPOINTS_UPDATE_PROFILE	(EndpointsService.class),
	/** Check device registration to endpoints */
	ENDPOINTS_CHECK_DEVICE		(EndpointsService.class),
	/** Send a request */
	ENDPOINTS_SEND_REQUEST		(EndpointsService.class),
	/** Accept a request */
	ENDPOINTS_SEND_RESPONSE		(EndpointsService.class),
	/** Send feedback to a response */
	ENDPOINTS_SEND_FEEDBACK		(EndpointsService.class),

	/** The request has been sent */
	ENDPOINTS_REQUEST_SENT		(HomeActivity.class),
	;	
	
	private Class<? extends Context> contextClass;
	
	IntentAction(Class<? extends Context> serviceClass) {
		this.contextClass = serviceClass;
	}
	
	public Class<? extends Context> getContextClass() {
		return contextClass;
	}
	
	public static IntentAction valueOf(Intent intent) {
		try {
			return valueOf(intent.getAction());
		} catch(Exception e) {
			Log.v(SolidalApp.TAG, "Unexpected action", e);
			return NULL_ACTION;
		}
	}
	
	/**
	 * Start the action
	 * 
	 * @param context
	 *				the current context
	 */ 
	public void start(Context context) {
		try {
			start(context, null, 0);
		} catch(Exception e) {}
	}
	
	/**
	 * Start the action
	 * 
	 * @param context
	 *				the current context 
	 * @param bundle
	 * 				the data to pass
	 * @param flags
	 * 				intent flags
	 */
	public void start(Context context, Bundle bundle, int flags) {
		if (contextClass==null)
			throw new IllegalArgumentException("contextClass cannot be null");
		Intent intent = new Intent(context, contextClass);
		intent.setAction(toString());
		intent.addFlags(flags);
		if(bundle!=null)
			intent.putExtras(bundle);
		if(Service.class.isAssignableFrom(contextClass))
			context.startService(intent);
		else if(Activity.class.isAssignableFrom(contextClass))
			context.startActivity(intent);
	}
		
}
